package string.leetcode.easy;

/**
 * @author bruin_du
 * @description 最多删除一个字符得到回文
 * @date 2022/8/9 20:55
 **/
public class OfferII019_ValidPalindrome {
    public boolean validPalindrome(String s) {
        int len = s.length();
        if (len == 1 || len == 2) return true;
        int begin = 0, end = len - 1;
        char[] arr = s.toCharArray();
        while (begin <= end) {
            if (arr[begin] != arr[end]) break;
            begin++;
            end--;
        }
        if (begin > end) return true;
        begin = 0;
        end = len - 1;
        while (begin <= end) {
            if (arr[begin] != arr[end]) {
                return palindrome(arr, begin + 1, end) || palindrome(arr, begin, end - 1);
            }
            begin++;
            end--;
        }
        return false;
    }

    public boolean palindrome(char[] arr,int begin,int end) {
        while (begin <= end) {
            if (arr[begin] != arr[end]) return false;
            begin++;
            end--;
        }
        return true;
    }
}
